﻿using Iworld.Backstage.Framework.AdministratorService;
using Iworld.Backstage.Framework.AuthorService;
using Iworld.Backstage.Framework.GameService;
using System;
using System.Collections.ObjectModel;
using System.Linq;
namespace Iworld.Backstage.Framework
{
    /// <summary>
    /// 提现记录视图模型
    /// </summary>
    public class WithdrawalsRecordsViewModel: ShowListViewModelBase<WithdrawalsRecordExport, AuthorServiceClient>
    {
        public DateTime? beginTime = null;
        public DateTime? endTime = null;
        string keyword = null;
        
        ObservableCollection<FilterBoxModel> statusGroups = new ObservableCollection<FilterBoxModel>();

        /// <summary>
        /// 类型
        /// </summary>
        public ObservableCollection<FilterBoxModel> StatusGroups
        {
            get { return statusGroups; }
            set
            {
                if (statusGroups == value) { return; }
                statusGroups = value;
                OnPropertyChanged("StatusGroups");
            }
        }

        /// <summary>
        /// 关键字
        /// </summary>
        public string Keyword
        {
            get
            {
                return keyword;
            }
            set
            {
                if (keyword != value)
                {
                    keyword = value;
                    OnPropertyChanged("Keyword");
                    Refresh(1);
                }
            }
        }

        /// <summary>
        /// 开始时间
        /// </summary>
        public DateTime? BeginTime
        {
            get
            {
                return beginTime;
            }
            set
            {
                if (beginTime != value)
                {
                    beginTime = value;
                    OnPropertyChanged("BeginTime");
                    this.Refresh(1);
                }
            }
        }

        /// <summary>
        /// 结束时间
        /// </summary>
        public DateTime? EndTime
        {
            get
            {
                return endTime;
            }
            set
            {
                if (endTime != value)
                {
                    endTime = value;
                    OnPropertyChanged("EndTime");
                    this.Refresh(1);
                }
            }
        }



        public WithdrawalsRecordsViewModel()
            : base(PageCategory.数据报表.ToString(),Page.提现记录.ToString())
        {
            client.GetWithdrawalsRecordExportsCompleted += this.WrapEventHandler<GetWithdrawalsRecordExportsCompletedEventArgs>(GetWithdrawalsRecordExportsCompleted);
            StatusGroups.Add(new FilterBoxModel("全部", null, Refresh, true));
            StatusGroups.Add(new FilterBoxModel(WithdrawalsStatus.处理中.ToString(), WithdrawalsStatus.处理中, Refresh));
            StatusGroups.Add(new FilterBoxModel(WithdrawalsStatus.提现成功.ToString(), WithdrawalsStatus.提现成功, Refresh));
            StatusGroups.Add(new FilterBoxModel(WithdrawalsStatus.失败.ToString(), WithdrawalsStatus.失败, Refresh));
        }

        void GetWithdrawalsRecordExportsCompleted(object sender, GetWithdrawalsRecordExportsCompletedEventArgs e)
        {
            if (e.Result.Success)
            {
                this.PageIndex = e.Result.PageIndex;
                this.TotalPage = e.Result.CountOfPage;
                UpdateLsit(e.Result.List);
            }
            else
            {
                ShowError(e.Result.Error);
            }
        }
 

        protected override void Refresh(object obj)
        {
            int pageIndex = obj == null ? this.PageIndex : Convert.ToInt32(obj);
            client.GetWithdrawalsRecordExportsAsync(new GetWithdrawalsRecordExportsImport()
            {
                BeginTime = this.BeginTime,
                EndTime = this.EndTime,
                PageIndex = pageIndex,
                Keyword=this.keyword, 
                Status=(WithdrawalsStatus?)StatusGroups.First(x => x.Selected).Value,
                Token = DataManager.GetValue<AdministratorExport>(DataKey.Administrator_Info).Token
            });
        }

        protected override void Reset(object obj)
        {
            this.BeginTime = null;
            this.EndTime = null;
            this.keyword = null;
            StatusGroups.First(x => x.Value == null).Selected = true;
            this.PageIndex = 1;
            Refresh(null);
        }
    }  
}
